﻿<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="SHZY-dialog SHZY-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        size="small"
        label-width="100px"
        label-position="right"
        :rules="rules"
      >
        <el-col :span="24">
          <el-form-item label="检验类型" prop="type">
            <el-select
              v-model="dataForm.type"
              placeholder="请选择"
              clearable
              required
              :style="{ width: '100%' }"
              filterable
            >
              <el-option
                v-for="(item, index) in typeOptions"
                :key="index"
                :label="item.fullName"
                :value="item.enCode"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="合并条件" prop="mergeConditions">
            <el-select
              v-model="dataForm.mergeConditions"
              placeholder="请选择"
              @change="change_mergeConditions(dataForm.mergeConditions)"
              required
              :style="{ width: '100%' }"
              multiple
              filterable
            >
              <el-option
                v-for="(item, index) in mergeConditionsOptions"
                :key="index"
                :label="item.fullName"
                :value="item.enCode"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="有效标志" prop="enabledMark">
            <el-switch
              v-model="dataForm.enabledMark"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="false">
          <el-form-item label="创建人" prop="creatorUserId">
            <el-input
              v-model="dataForm.creatorUserId"
              placeholder="系统自动生成"
              readonly
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="false">
          <el-form-item label="创建时间" prop="creatorTime">
            <el-input
              v-model="dataForm.creatorTime"
              placeholder="系统自动生成"
              readonly
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="false">
          <el-form-item label="更新人" prop="lastModifyUserId">
            <el-input
              v-model="dataForm.lastModifyUserId"
              placeholder="系统自动生成"
              readonly
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="false">
          <el-form-item label="更新时间" prop="lastModifyTime">
            <el-input
              v-model="dataForm.lastModifyTime"
              placeholder="系统自动生成"
              readonly
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import Enumerable from "linq";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
export default {
  components: {},
  props: [],
  data() {
    return {
      btnLoading: false,
      loading: false,
      visible: false,
      dataForm: {
        id: "",
        type: undefined,
        mergeConditionsName: undefined,
        mergeConditions: ["BatchNumber", "Supplier", "Material"],
        enabledMark: 1,
        creatorUserId: undefined,
        creatorTime: undefined,
        lastModifyUserId: undefined,
        lastModifyTime: undefined,
      },
      rules: {
        type: [
          {
            required: true,
            message: "请选择检验类型",
            trigger: '"change"',
          },
        ],
        mergeConditions: [
          {
            required: true,
            message: "请选择合并条件",
            trigger: '"change"',
          },
        ],
      },
      typeOptions: [],
      mergeConditionsOptions: [],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.gettypeOptions();
    //this.getmergeConditionsOptions();
  },
  mounted() {},
  methods: {
    change_mergeConditions(data) {
      if (!this.mergeConditionsOptions) return;

      let list = Enumerable.from(this.dataForm.mergeConditions);
      this.dataForm.mergeConditionsName = Enumerable.from(
        this.mergeConditionsOptions
      )
        .where((w) => list.contains(w.enCode))
        .select((s) => s.fullName)
        .toArray()
        .join(",");

      //console.log(this.dataForm.mergeConditionsName);
    },
    gettypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", { sort: "BusinessType" })
        .then((res) => {
          this.typeOptions = res;
        });
      this.$store
        .dispatch("base/getDictionaryData", {
          sort: "BaseData.Inspcombinesetting.MergeConditions",
        })
        .then((res) => {
          this.mergeConditionsOptions = res;
        });
      // getDictionaryDataSelector('448687444825473541').then(res =>
      // {
      //     console.log(`res.data.list:`+res.data.list)
      //     this.typeOptions = res.data.list
      // });
    },
    // getmergeConditionsOptions()
    // {
    //     this.$store.dispatch('base/getDictionaryData', { sort: 'BaseData.Inspcombinesetting.MergeConditions' }).then((res) => {
    //         this.mergeConditionsOptions = res

    // 	})
    //     // getDictionaryDataSelector('453033691602491973').then(res =>
    //     // {
    //     //     this.mergeConditionsOptions = res.data.list
    //     //     let list = Enumerable.from(this.dataForm.mergeConditions);
    //     //     this.dataForm.mergeConditionsName = Enumerable.from(res.data.list)
    //     //         .where(w => list.contains(w.enCode))
    //     //         .select(s => s.fullName).toArray().join(",");

    //     //      console.log(this.dataForm.mergeConditionsName);
    //     // });
    // },
    goBack() {
      this.$emit("refresh");
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          request({
            url: "/api/BaseData/Inspcombinesetting/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataForm = res.data;
          });
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          if (!this.dataForm.id) {
            request({
              url: `/api/BaseData/Inspcombinesetting`,
              method: "post",
              data: this.dataForm,
            })
              .then((res) => {
                this.$message({
                  message: res.msg,
                  type: "success",
                  duration: 1000,
                  onClose: () => {
                    this.btnLoading = false;
                    (this.visible = false), this.$emit("refresh", true);
                  },
                });
              })
              .catch((ex) => {
                this.btnLoading = false;
                console.log(ex);
              });
          } else {
            let list = Enumerable.from(this.dataForm.mergeConditions);
            this.dataForm.mergeConditionsName = Enumerable.from(
              this.mergeConditionsOptions
            )
              .where((w) => list.contains(w.enCode))
              .select((s) => s.fullName)
              .toArray()
              .join(",");

            request({
              url: "/api/BaseData/Inspcombinesetting/" + this.dataForm.id,
              method: "PUT",
              data: this.dataForm,
            }).then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.btnLoading = false;
                  this.visible = false;
                  this.$emit("refresh", true);
                },
              });
            });
          }
        }
      });
    },
  },
};
</script>
